<?php

namespace App\Repository;
use App\Models\Base\Resource;
use App\Models\Role\Role;
use App\Models\Role\RoleMenuBind;
use App\Models\Role\RoleResourceBind;
use App\Models\User\User;

/**
 * 角色数据仓储类
 *
 * @package App\Http\Repository
 * @author WangBin
 * @mail: <849850352@qq.com>
 */
class RoleRepository extends BaseRepository
{
    /**
     * 获取角色
     *
     * @param int $roleId
     * @return
     */
    public function getById(int $roleId)
    {
        return Role::query()->where('role_id', $roleId)->first();
    }

    /**
     * 获取多个角色信息
     *
     * @param array $roleIds
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getByIds(array $roleIds)
    {
        return Role::query()->whereIn('role_id', $roleIds)->get();
    }

    /**
     * 获取角色绑定的菜单
     *
     * @param int $roleId
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getRoleMenu(int $roleId)
    {
        return RoleMenuBind::query()->where('role_id', $roleId)->get();
    }

    /**
     * 获取角色列表
     *
     * @param array $filter
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getList(array $filter = [])
    {
        $query = Role::query()->orderByDesc('created_at');
        if(!empty($filter['name'])) {
            $query->where('role_name', 'like', '%' . $filter['name'] . '%');
        }
        return $query;
    }

    /**
     * 保存角色
     *
     * @param array $data
     * @param int $roleId
     * @return int|mixed
     */
    public function update(array $data, $roleId = 0)
    {
        $query = $roleId ? Role::find($roleId) : new Role();
        foreach ($data as $field => $value) {
            $query->{$field} = $value;
        }
        return $query->save() ? $query->role_id : 0;
    }

    /**
     * 检查角色编号已存在
     *
     * @param string $roleBn
     * @param int $roleId
     * @return bool
     */
    public function checkRoleBn(string $roleBn, int $roleId = 0)
    {
        $query = Role::query()->where('role_bn', $roleBn);
        if($roleId) {
            $query->where('role_id', '!=', $roleId);
        }
        return $query->exists();
    }

    /**
     * 检查角色编号已存在
     *
     * @param string $roleName
     * @param int $roleId
     * @return bool
     */
    public function checkRoleName(string $roleName, int $roleId = 0)
    {
        $query = Role::query()->where('role_name', $roleName);
        if($roleId) {
            $query->where('role_id', '!=', $roleId);
        }
        return $query->exists();
    }

    /**
     * 删除角色
     *
     * @param int $roleId
     * @return mixed
     */
    public function delete(int $roleId)
    {
        return Role::query()->where('role_id', $roleId)->delete();
    }

    /**
     * 获取角色的资源编码
     *
     * @param array $roleIds
     * @return \Illuminate\Support\Collection
     */
    public function getResourceByIds(array $roleIds)
    {
        $resourceIds = RoleResourceBind::query()->whereIn('role_id', $roleIds)->get()->pluck('resource_id');
        $resourceCode = Resource::query()->whereIn('resource_id', $resourceIds)->get()->pluck('resource_code');
        return $resourceCode;
    }


    /**
     * 绑定角色菜单
     *
     * @param array $menuIds
     * @param int $roleId
     */
    public function updateMenu(array $menuIds, int $roleId)
    {
        foreach ($menuIds as $menuId) {
            RoleMenuBind::query()->insert(['role_id' => $roleId, 'menu_id' => $menuId]);
        }
    }
}
